package com.koron.common.core.business.common.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.koron.common.core.business.common.entity.TOrderNoMax;
import com.koron.common.core.business.common.mapper.TOrderNoMaxMapper;
import com.koron.common.core.business.common.service.TOrderNoMaxService;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * @author jzp
 * @since 2021/9/22
 */
@Service
public class TOrderNoMaxServiceImpl implements TOrderNoMaxService {

    private final String DATE_FORMAT = "yyyyMMdd";

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public TOrderNoMax getByKey(String key, String dateFormat) {
        return peekNoMax(key, dateFormat, 1);
    }

    @Override
    public TOrderNoMax peekNoMax(String key, String dateFormat, int step) {
        PageHelper.clearPage();
        // 如果格式为空则使用默认值
        if (StrUtil.isEmpty(dateFormat)) {
            dateFormat = this.DATE_FORMAT;
        }
        try {
            while (true) {
                Boolean result = stringRedisTemplate.opsForValue().setIfAbsent(key, key + dateFormat, 10, TimeUnit.SECONDS);
                // 如果设置成功，获取编码
                if (Boolean.TRUE.equals(result)) {
                    SimpleDateFormat ft = new SimpleDateFormat(dateFormat);
                    try (SessionFactory factory = new SessionFactory()) {
                        TOrderNoMax tOrderNoMax = factory.getMapper(TOrderNoMaxMapper.class).selectByCode(key);
                        String currDate = ft.format(new Date()); // 当前日期

                        // 没有这个code
                        if (tOrderNoMax == null) {
                            tOrderNoMax = new TOrderNoMax();
                            tOrderNoMax.setCode(key);
                            tOrderNoMax.setDate(currDate);
                            tOrderNoMax.setMaxValue(step);
                            factory.getMapper(TOrderNoMaxMapper.class).insertSelective(tOrderNoMax);
                        } else {
                            // 如果是当前日期直接+step
                            if (currDate.equals(tOrderNoMax.getDate())) {
                                tOrderNoMax.setMaxValue(tOrderNoMax.getMaxValue() + step);
                            } else {
                                tOrderNoMax.setMaxValue(step); // 日期归step
                                tOrderNoMax.setDate(currDate);
                            }
                            factory.getMapper(TOrderNoMaxMapper.class).updateByPrimaryKeySelective(tOrderNoMax);
                        }
                        return tOrderNoMax;
                    }
                }
                // 否则CAS
            }
        }finally {
            // 释放锁
            stringRedisTemplate.delete(key);
        }
    }
}
